package org.itboys.mobile.service.mongo.mobile.cookbook;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.cookbook.KitchenCookBookDto;
import org.itboys.mobile.dto.cookbook.KitchenCookBookProductDto;
import org.itboys.mobile.entity.mongo.cookbook.KitchenCookBook;
import org.itboys.mobile.entity.mongo.cookbook.KitchenCookBookProduct;
import org.itboys.mobile.service.mongo.mobile.collection.SundayMobileCollectionService;
import org.itboys.mobile.service.mongo.mobile.product.KitchenMobileProductCookBookService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * 作者：zzq
 * 时间：2017年5月10日
 * 描述：花果山（农厨）_移动端_菜谱_service
 */
@Service
public class KitchenMobileCookBookService extends BaseMongoService<KitchenCookBook> {
    @Autowired
    private KitchenMobileCookBookProductService bookProductService;
    @Autowired
    private KitchenMobileProductCookBookService productCookBookService;
    @Autowired
    private SundayMobileCollectionService collectionService;
    @Resource(name = "kitchenDs")
    private MongoDataSource kitchenDataSource;

    @Autowired
    protected ResourceHolder resourceHolder;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }

    @Override
    protected Class<KitchenCookBook> getEntityClass() {
        return KitchenCookBook.class;
    }
/**
     * 分页查询菜谱
     * @param request
     * @return
     */
    public List<KitchenCookBookDto> selectBookCook(HttpServletRequest request, Long memberId){
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request,"");
        param.put("isDeleted",0);
        Map<String, String> containsparam = ParamUtil.packageMongoVagueParam(request,"name","viceName");
        return packageDtoMuilt(super.containpageQuery(request,containsparam,param).getData(),memberId);
    }
    /**
     * 查询单个DTO
     **/
    public KitchenCookBookDto findOne(long id, Long memberId){
        return packageDtoSingle(super.getById(id),true,null,true,true,memberId);
    }

    /**
     * 组装单个dto
     * @param cookBook
     * @return
     */
    public KitchenCookBookDto packageDtoSingle(KitchenCookBook cookBook, boolean isPackageProduct, List<KitchenCookBookProductDto> cookBookProductDtos,boolean isCheckCollection,boolean isPackageCookBooks,Long memberId){

        Integer isCollection=0;
        if(isPackageProduct){
            cookBookProductDtos=bookProductService.selectBookProduct(cookBook.getId());
        }
        if(isCheckCollection){
            isCollection=collectionService.checkIsCollection(memberId, SundayCommonConstants.collection_type_cookbook,cookBook.getId());
        }
        List<Long> cookBookIds=new ArrayList<Long>();
        cookBookIds.add(cookBook.getId());
        String descUrl = resourceHolder.getStringValue("mobileRoot")+"/kitchen/mobile/cookbook/desc?id="+cookBook.getId();
        String imageRoot=resourceHolder.getStringValue("imgRoot");
        //推荐食谱。最多5个
        List<Map<String,Object>> cookBooks =new ArrayList<Map<String,Object>>();
        if(isPackageCookBooks){
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("isIndexRecommend",1);
            param.put("limit",6);
            param.put("offset",0);
            List<KitchenCookBook> books = super.list(param);
            for(KitchenCookBook book:books){
                Map<String,Object> c = new HashMap<String,Object>();
                c.put("id",book.getId());
                c.put("name",book.getName());
                c.put("viceName",book.getViceName());
                c.put("image",imageRoot+book.getImage());
                cookBooks.add(c);
            }
        }
       return new KitchenCookBookDto(
               cookBook.getId(), cookBook.getCreateTime(), cookBook.getCreateDate(),
               cookBook.getViceName(),cookBook.getName(),cookBook.getTime(),
               cookBook.getDifficulty(),cookBook.getDetail(), imageRoot+cookBook.getImage(),isCollection,
               descUrl,cookBookProductDtos,cookBooks);
         }

    /**
     * 组装多个dto
     * @param cookBooks
     * @return
     */
    public List<KitchenCookBookDto> packageDtoMuilt(List<KitchenCookBook> cookBooks,Long memberId){
        List<KitchenCookBookDto> dtos=new ArrayList<KitchenCookBookDto>();
            for(KitchenCookBook book:cookBooks){
                dtos.add(packageDtoSingle(book,false,null,false,false,memberId));
            }
        return dtos;
    }


}
